﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;

namespace Spaanjaars.ContactManager.Repository.EF.DataContextStorage
{
    public class ThreadDataContextStorageContainer : IDataContextStorageContainer 
    {    
        private static readonly Hashtable _contcatsContexts = new Hashtable();

        public ContactsContext GetDataContext()
        {
            ContactsContext contactsContext = null;

            if (_contcatsContexts.Contains(GetThreadName()))
                contactsContext = (ContactsContext)_contcatsContexts[GetThreadName()];           

            return contactsContext;
        }

        public void Store(ContactsContext libraryDataContext)
        {
            if (_contcatsContexts.Contains(GetThreadName()))
                _contcatsContexts[GetThreadName()] = libraryDataContext;
            else
                _contcatsContexts.Add(GetThreadName(), libraryDataContext);           
        }

        private static string GetThreadName()
        {
            return Thread.CurrentThread.Name;
        }     
    }
}
